package com.oa.ylms.apps.business.archives.impl;

import java.util.List;

import com.oa.ylms.apps.business.archives.IArchiveInfoService;
import com.oa.ylms.apps.business.archives.IRecordInfoService;
import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.IRecordCategroyStates;
import com.oa.ylms.apps.dao.archives.IStaffInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;

public class StaffInfoServiceImpl implements IStaffInfoService {
	private IStaffInfoDao infoDao;
	private StaffInfoValue staffInfo;
	private ArchiveInfoValue archiveInfo;
	private IArchiveInfoService archiveInfoService;
	private ISectionInfoService sectionInfoService;
	private IRecordInfoService recordService;
	
	public IPostInfoService getPostService() {
		return postService;
	}

	public void setPostService(IPostInfoService postService) {
		this.postService = postService;
	}

	private IPostInfoService postService;
	
	@Override
	public int addStaffInfo(int archiveId, String staffNo, String staffPwd)
			throws Exception {
		staffInfo.setArchiveInfo(archiveInfoService.queryByArchiveId(archiveId));
		if(staffInfo.getArchiveInfo() != null){
			staffInfo.setStaffNo(staffNo);
			staffInfo.setStaffPwd(staffPwd);
			staffInfo.setStates(IDEDStates.ENABLE);
			return infoDao.addStaffInfo(staffInfo);
		}
		return 0;
	}

	@Override
	public boolean deleteStaffInfo(String staffIds, StaffInfoValue handleStaffInfo) throws Exception {
		String[] staffIdStr = staffIds.split(",");
		for(String staffId : staffIdStr){
			staffInfo = queryByStaffId(Integer.parseInt(staffId));
			if(staffInfo != null){
				staffInfo.setStates(IDEDStates.DELETE);
				infoDao.editStaffInfo(staffInfo);
				String messages ="离职记录：该人员已离职, 操作者："+handleStaffInfo.getArchiveInfo().getStaffName();
				recordService.addRecordInfo(IRecordCategroyStates.OFF, staffInfo.getArchiveInfo().getArchiveId(), messages);
			}else{
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean modifyStaffPwd(int staffId, String staffPwd)
			throws Exception {
		staffInfo = queryByStaffId(staffId);
		if(staffInfo != null){
			staffInfo.setStaffPwd(staffPwd);
			return infoDao.editStaffInfo(staffInfo);
		}
		return false;
	}
	
	@Override
	public boolean modifyStaffInfo(int staffId, String staffNo)
			throws Exception {
		staffInfo = queryByStaffId(staffId);
		if(staffInfo != null){
			staffInfo.setStaffNo(staffNo);
			return infoDao.editStaffInfo(staffInfo);
		}
		return false;
	}
	
	@Override
	public StaffInfoValue queryByStaffId(int staffId) throws Exception {
		staffInfo.setStaffId(staffId);
		return infoDao.queryByStaffId(staffInfo);
	}

	@Override
	public List<StaffInfoValue> queryStaffInfoBySectionId(int sectionId)
			throws Exception {
		try{
			return infoDao.queryBySectionId(sectionInfoService.queryBySectionId(sectionId));
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public List<StaffInfoValue> queryStaffInfoWithPage(String staffNo,Integer postId,String staffName, Integer states, int rows, int pageNo) throws Exception {
		if(staffNo!=null){
			staffInfo.setStaffNo(staffNo);
		}
		if(postId!=null){
			archiveInfo.setPostInfo(postService.queryByPostId(postId));
		}
		if(staffName!=null){
			archiveInfo.setStaffName(staffName);
		}
		if(states!=null){
			staffInfo.setStates(states);
		}
		return infoDao.queryStaffInfo(staffInfo,archiveInfo, rows, pageNo);
	}

	@Override
	public boolean toDisable(int staffId,StaffInfoValue handleStaffInfo) throws Exception {
		staffInfo = queryByStaffId(staffId);
		if(staffInfo != null){
			staffInfo.setStates(IDEDStates.DISABLE);
			if(infoDao.editStaffInfo(staffInfo)){
				String messages ="停职记录：该人员已被停职, 操作者："+handleStaffInfo.getArchiveInfo().getStaffName();
				recordService.addRecordInfo(IRecordCategroyStates.STOP, staffInfo.getArchiveInfo().getArchiveId(), messages);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean toEnable(int staffId,StaffInfoValue handleStaffInfo) throws Exception {
		staffInfo = queryByStaffId(staffId);
		if(staffInfo != null){
			staffInfo.setStates(IDEDStates.ENABLE);
			if(infoDao.editStaffInfo(staffInfo)){
				String messages ="入职记录：该人员已重新入职到"+staffInfo.getArchiveInfo().getPostInfo().getSectionInfo().getSectionName()+"部门, 操作者："+handleStaffInfo.getArchiveInfo().getStaffName();
				recordService.addRecordInfo(IRecordCategroyStates.ON, staffInfo.getArchiveInfo().getArchiveId(), messages);
				return true;
			}
		}
		return false;
	}

	@Override
	public StaffInfoValue verifyStaffInfo(String staffNo, String staffPwd)
			throws Exception {
		if(staffNo != null)
		staffInfo.setStaffNo(staffNo);
		if(staffPwd != null)
		staffInfo.setStaffPwd(staffPwd);
		for(StaffInfoValue siv : infoDao.verifyStaffInfo(staffInfo)){
			if(siv != null)
				return siv;
		}
		return null;
	}
	
	@Override
	public int countTPage(String staffNo,Integer postId,String staffName, Integer states, int rows) throws Exception {
		if(staffNo!=null){
			staffInfo.setStaffNo(staffNo);
		}
	if(postId!=null){
			archiveInfo.setPostInfo(postService.queryByPostId(postId));
		}
		if(staffName!=null){
			archiveInfo.setStaffName(staffName);
		}
		if(states!=null){
			staffInfo.setStates(states);
		}
		return infoDao.countTPage(staffInfo,archiveInfo, rows);
	}
	
	@Override
	public int getTotalNumber(){
		return infoDao.getTotalNumber();
	}
	
	

	public IStaffInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IStaffInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public StaffInfoValue getStaffInfo() {
		return staffInfo;
	}

	public void setStaffInfo(StaffInfoValue staffInfo) {
		this.staffInfo = staffInfo;
	}

	public void setArchiveInfoService(IArchiveInfoService archiveInfoService) {
		this.archiveInfoService = archiveInfoService;
	}

	public IArchiveInfoService getArchiveInfoService() {
		return archiveInfoService;
	}

	public void setSectionInfoService(ISectionInfoService sectionInfoService) {
		this.sectionInfoService = sectionInfoService;
	}

	public ISectionInfoService getSectionInfoService() {
		return sectionInfoService;
	}

	public void setArchiveInfo(ArchiveInfoValue archiveInfo) {
		this.archiveInfo = archiveInfo;
	}

	public ArchiveInfoValue getArchiveInfo() {
		return archiveInfo;
	}

	public void setRecordService(IRecordInfoService recordService) {
		this.recordService = recordService;
	}

	public IRecordInfoService getRecordService() {
		return recordService;
	}

}
